package com.example.xm.service.impl;

import com.example.xm.entity.Huiyuanentity;
import com.example.xm.entity.Userentity;
import com.example.xm.entity.dto.UserDto;
import com.example.xm.entity.vo.UserVo;
import com.example.xm.exception.SerceiceException;
import com.example.xm.mapper.UserMapper;
import com.example.xm.service.HuiyuanService;
import com.example.xm.service.UserService;
import com.example.xm.utils.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author tong
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-04-13 12:37:40
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired(required = false)
    UserMapper userMapper;

@Autowired
    HuiyuanService huiyuanService;
    @Override
    public void reg(Userentity user) {
        Userentity userentity= userMapper.selectByName(user.getName());
        if(userentity!=null){
            throw new SerceiceException(400,"用户名已存在");
        }
    userMapper.insert(user);
    }

    @Override
    public String login(String name, String password) {
        Userentity userentity= userMapper.selectByName(name);
        if(userentity==null){
            throw new SerceiceException(400,"用户不存在");
        }
        if(!userentity.getPassword().equals(password)){
            throw new SerceiceException(400,"密码不正确");
        }
        System.out.println(userentity.getType()+"-------------");
        if(userentity.getType().equals("1")){
            throw new SerceiceException(400,"用户不能登录");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",userentity.getId());
        String token = JWTUtils.getToken(map);
        return token;
    }

    @Override
    public Userentity getInfo(String token) {
        return null;
    }

    @Override
    public void delete(int[] intArray) {
userMapper.delete(intArray);
    }

    @Override
    public void update(Userentity user) {
userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public PageInfo<Userentity> list(UserDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getSize());
      List<Userentity> list= userMapper.selectList(dto);
        PageInfo<Userentity> userentityPageInfo = new PageInfo<>(list);
        return userentityPageInfo;
    }

    @Override
    public UserVo weblogin(String name, String password) {
        Userentity userentity= userMapper.selectByName(name);
        if(userentity==null){
            throw new SerceiceException(400,"用户不存在");
        }
        if(!userentity.getPassword().equals(password)){
            throw new SerceiceException(400,"密码不正确");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userentity,userVo);
       Huiyuanentity huiyuanentity= huiyuanService.getById(userentity.getHuiyuanId());
       if(huiyuanentity!=null){

       userVo.setZhekou(huiyuanentity.getZhekou());
       }
       return userVo;
    }
}




